$(function () {
	$("#jqGrid").jqGrid({
		url: baseURL + 'sys/buserinfo/noWorthUser',
		datatype: "json",
		async:false,
		colModel: [
			{ label: '用户编号', name: 'userId', index: 'user_id',align: "center", width: 50, key: true,formatter:function(value,options,rowData) {
					return "<a onclick='window.open(\"../sys/buserinfo.html?userId=" + value + "\")'>" + value + "</a>";
				}
			},
			{ label: '用户昵称', name: 'nickName', index: 'nick_name',align: "center", width: 80 },
			{ label: '微信昵称', name: 'userName', index: 'user_name', align: "center", width: 80},
			{ label: '关注品牌', name: 'brandName', index: 'brandName',align: "center", width: 80 },
			{ label: '用户头像', name: 'avatarUrl', index: 'avatar_url', width: 80,align: "center", sortable: false, editable: false, formatter: function (value,options,rowData) {
					var url="https://lktvtool.oss-cn-shenzhen.aliyuncs.com/icon/236A5FF0-52DE-4644-B66C-E9555C97693D.png";
					if(value==null || value==""){
						return "<img src='" + url + "' height='60' />";
					}else {
						return "<img src='" + value + "' height='60' />";
					}
				}
			},
			{ label: '二维码', name: 'qrcodeUrl', index: 'qrcode_url', width: 80,align: "center", sortable: false, editable: false, formatter: function (value,options,rowData) {
					let userId = rowData["userId"];
					if(value==null || value==""){
						return "";
					}else {
						return "<img src='" + value + "' height='60' onclick='vm.queryQrcode(this,"+userId+")' />";
					}
				}
			},
			{ label: '手机', name: 'telNum', index: 'tel_num', width: 80,align: "center", sortable: false, editable: false, formatter: function (value,options,rowData) {
					if (value==null || value =="") {
						return "";
					} else {
						return "<p onclick='vm.copyTelNum(this,\""+value+"\")'>"+value+"</p>"
					}
				}
			},
			{ label: '邀请人数', name: 'refererTotal',dataType: "int", sortable: true, index: 'referer_total',align: "center", width: 80 ,formatter:function(value,options,rowData){
					var userId=rowData["userId"];
					return "<a  onclick='vm.referer("+userId+")'>"+value+"</a>";
				}
			},
			{ label: '待提现', name: 'waitIncome',dataType: "int", sortable: true, index: 'waitIncome',align: "center", width: 80},
			{ label: '推荐人', name: 'referer', index: 'referer',align: "center", width: 80 ,formatter:function(value,options,rowData){
					if( value>0 ){
						return "<a onclick='window.open(\"../sys/buserinfo.html?userId=" + value + "\")'>" + value + "</a>";
					}else{
						return '';
					}
				}
			},
			{ label: '会员状态', name: 'vipStatus', index: 'vip_status',align: "center", width: 80 ,formatter:function(value,options,rowData){
					if( value===1 ){
						return "<p style='color: #1AE61A'>" + '会员' + "</>";;
					}else{
						return '非会员';
					}
				}
			},
			{ label: '公众号', name: 'gzhName', index: 'gzh_code',align: "center", width:60 },
			{ label: '关注时间', name: 'subscribeTime', index: 'unSubscribe_time',align: "center", width: 80 },
			{ label: '操作', name: '', index: '',align: "center", width: 80, formatter(value,options,rowData) {
					let userId = rowData["userId"];
					return "<a class='btn btn-primary' onclick='vm.sendTemplateMessage("+userId+")'>发送消息</a>";
				}
			},
		],
		viewrecords: true,
		height: "100%",
		rowNum: 8,
		rowList : [8,30,50],
		rownumbers: true,
		rownumWidth: 25,
		autowidth:true,
		multiselect: true,
		pager: "#jqGridPager",
		jsonReader : {
			root: "page.list",
			page: "page.currPage",
			total: "page.totalPage",
			records: "page.totalCount"
		},
		postData:{"userId": vm.q.userId, "nickName": vm.q.nickName, "referer": vm.q.referer, "gzhCode": vm.q.gzhCode, "blockState": vm.q.blockState, "brandName": vm.q.brandName, vipStatus: vm.q.vipStatus},
		prmNames : {
			page:"page",
			rows:"limit",
			order: "order"
		},
		gridComplete:function(){
			//隐藏grid底部滚动条
			$("#jqGrid").closest(".ui-jqgrid-bdiv").css({ "overflow-x" : "hidden" });
		}
	});

	$.ajax({
		type: "POST",
		async:false,
		url: baseURL + 'sys/buserinfo/getUserGzhList',
		contentType: "application/json",
		success: function(r){
			if(r.code==0){
				//vm.bbrandinfo=r.brandInfo;
				$("#selectUserGzhList").append(
					"<option value=''>全部平台</option>");
				for(var i=0;i<r.userGzhList.length;i++){
					$("#selectUserGzhList").append(
						"<option value=" + r.userGzhList[i].gzhCode +">"+ r.userGzhList[i].gzhName+"</option>");
				}
			}
		}
	});


	$.ajax({
		type: "POST",
		async:false,
		url: baseURL + 'sys/bbrandinfo/queryAll',
		contentType: "application/json",
		success: function(r){
			if(r.code==0){
				//vm.bbrandinfo=r.brandInfo;
				for(var i=0;i<r.brandInfo.length;i++){
					$("#brandName").append(
						"<option value=" + r.brandInfo[i].brandName +">"+"</option>");
				}
			}
		}
	});
});

var vm = new Vue({
	el:'#rrapp',
	data:{
		q:{
			userId: null,
			nickName: null,
			referer:null,
			gzhCode: '',
			blockState: '',
			brandName: null,
			vipStatus: ''
		},
		showList: true,
		title: null,
		layero: null,
	},
	methods: {
		query: function () {
			vm.q.brandName=$("#brandNames").val();
			vm.reload();
		},
		/**
		 * 查看用户二维码
		 * @param e
		 */
		queryQrcode: function (e,userId) {
			let src = $(e).attr("src");
			if (!src) {
				return;
			}

			$("#userQrcodeUrl").attr("src", src);

			let index = layer.open({
				type: 1,
				title: '当前用户ID: '+userId,
				maxmin: true,
				shadeClose: true, //点击遮罩关闭层
				area : ['480px' , '500px'],
				content: $("#userQrcode").html(),
				success: function (layero, index) {
					vm.layero = layero;
				}
			});
		},
		/**
		 * 复制电话号码
		 * @param e
		 */
		copyTelNum: function (e,text) {
			if (text == null || text == "") {
				return;
			}
			// 复制电话号码到剪贴板
			var input = document.createElement("input");//创建input对象
			var currentFocus = document.activeElement;//当前获得焦点的元素
			document.body.appendChild(input);//添加元素
			input.value = text;
			input.focus();
			if(input.setSelectionRange)
				input.setSelectionRange(0, input.value.length);//获取光标起始位置到结束位置
			else
				input.select();
			try {
				document.execCommand("copy");//执行复制
			} catch(eo) {

			}
			document.body.removeChild(input);//删除元素
			currentFocus.focus();

			let flag = false;

			// 防止多次显示
			if (!flag) {
				// 创建复制成功后的 p 标签
				let p = $("<p style='color: red'>复制成功</p>");
				$(e).after(p);// 加到后面
				flag = true;

				setTimeout(function () {
					$(e).next().remove();
					flag = false;
				},2000)
			}
		},
		sendTemplateMessage: function (userId) {
			// 发送模板消息
			$.ajax({
				type: "POST",
				url: baseURL + "message/sendZWAMessage/" + userId,
				contentType: "application/json",
				success: function (r) {
					if (r.code == 0) {
						alert("模板消息发送成功");
					}
				}
			});
		},
		reload: function (event) {
			vm.showList = true;
			var page = $("#jqGrid").jqGrid('getGridParam','page');
			$("#jqGrid").jqGrid('setGridParam',{
				postData:{"userId": vm.q.userId, "nickName": vm.q.nickName, "referer": vm.q.referer, "gzhCode": vm.q.gzhCode, "blockState": vm.q.blockState, "brandName": vm.q.brandName, vipStatus: vm.q.vipStatus},
                page:page 
            }).trigger("reloadGrid");
		}
	}
});